package day07.hashMap_hashTable;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <>
 * <详细描述>
 *
 * @Author WANG.SONG.
 * @Date 2019/3/1 0001 21:55
 * @see
 */
public class HashMapAndHashTable {

    /**
     *<KEY允许有一个null ,Value 可以有多个null>
     * 如果key相同，后面的K V会覆盖前面K V
     * 遍历集合从表头开始
     * 线程不安全
     * 无序
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    public static void hashMap(){
        Map<String,String> map=new HashMap<>();
        map.put(null,null);
        /**集合中不存在**/
        map.put(null,"one");
        map.put("4",null);
        map.put("3","three");
        map.put("2","four");
        map.put("100","four");
        map.put("80","four");
        map.put("89","four");
        map.put("66","four");


        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()){
            String k =it.next();
            System.out.println("key:"+k);
        }

    }
    /**
     *<线程安全的，与hashMap关系类似ArrayList Vector关系>
     * 遍历集合从表尾开始
     *
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    public static void hashTable(){

        Map<String,String> map= new Hashtable<>();
        /**K 或者V 存放null都会报NPE异常**/
       /* map.put(null,null);
        map.put(null,"one");
        map.put("2",null);*/
        map.put("3","three");
        map.put("4","four");

        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()){
            String k =it.next();

            System.out.println("hashTable_key:"+k);
            System.out.println("hashTable_value:"+map.get(k));
        }
    }
    /**
     *<线程安全的hashMap>
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    public static void concurrentHashMap(){
        Map<String,String> map = new ConcurrentHashMap<>();
        map.put("1","one");
        map.put("2","two");
        map.put("3","three");
        map.put("4","four");

        for (String m : map.keySet()) {
            System.out.println("concurrentHashMap_key:"+m);
            System.out.println("concurrentHashMap_value:"+map.get(m));
        }
    }
}
